// packageE/others/case_library/home/caseLibraryHome.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    category_index:'',
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdata();
  },
  changeCategory(e){
    let id = e.currentTarget.dataset.id;
    console.log(id);
    this.setData({
      category_index : id
    });
    this.getList(id);
  },
  gotoCaseDetail(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageE/others/case_library/case_detail/caseLibraryDetail?id='+id,
    });
  },
  gotoSpecial(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageE/others/case_library/subject_detail/caseLibrarySubjectDetail?id='+id
    });
  },
  gotoOther(e){
    console.log(e);
    let name = e.currentTarget.dataset.name;
    if(name == "new"){
      wx.navigateTo({
        url: '/packageE/others/case_library/new_mobile/caseLibraryNewMobile?name=caseLibraryNewMobile'
      });
    }else if(name == "category"){
      wx.navigateTo({
        url: '/packageE/others/case_library/category/caseLibraryCategory'
      });
    }else if(name == "ranking"){
      wx.navigateTo({
        url: '/packageE/others/case_library/ranking_mobile/caseLibraryRankingMobile'
      });
    }else if(name == "area"){
      wx.navigateTo({
        url: '/packageE/others/case_library/new_mobile/caseLibraryNewMobile?name=caseLibraryAreaMobile'
      });
    }else if(name == "subjectList"){
      wx.navigateTo({
        url: '/packageE/others/case_library/subject_list/caseLibrarySubjectList'
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.current_page >= this.data.last_page){
      return;
    }else{
      let pages = this.data.page + 1;
      this.setData({
        page : pages
      });
      this.getMoreData();

    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goto(e){
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url,
    })
  },
  getdata(){
    var that = this;
    let urlStr = app.getNetAddresss('plugin.case-library.api.mobile.home.index');
    app._postNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if(res.data.newCase) {
            res.data.newCase.map((item)=> {
              item.created_at = item.created_at.split(' ')[0];
            });
          }
          that.setData({
            view_show:true,
            banner:res.data.banner,
            newCase:res.data.newCase,
            special:res.data.special,
            hotCategory:res.data.hotCategory,
            advert:res.data.advert,
            ad1:res.data.advert.ad1.length && res.data.advert.ad1.length == 0 ? '' : res.data.advert.ad1,
            ad2:res.data.advert.ad2.length && res.data.advert.ad2.length == 0 ? '' : res.data.advert.ad2,
            hotCasesList:res.data.hotCases.data,
            current_page:res.data.hotCases.current_page,
            last_page:res.data.hotCases.last_page,
          });
          if (that.data.hotCategory.length >= 1) {
            that.setData({
              category_index: that.data.hotCategory[0].id
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getList(id){
    wx.showLoading({
      title: '加载中',
    });
    var that = this;
    let urlStr = app.getNetAddresss('plugin.case-library.api.mobile.home.home-cut-category');
    this.setData({
      page:1
    });
    app._postNetWork({
      url: urlStr,
      data:{
        cid:id
      },
      success: function (resdata) {
        wx.hideLoading({
          success: (res) => {},
        });
        var res = resdata.data;
        if (res.result == 1) {
          that.data.hotCasesList = [];
          that.setData({
            touchId:id,
            hotCasesList : res.data.hotCases.data,
            current_page : res.data.hotCases.current_page,
            last_page : res.data.hotCases.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.hideLoading({
          success: (res) => {},
        });
      }
    });
  },
  getMoreData(){
    wx.showLoading({
      title: '加载中',
    });
    var that = this;
    let urlStr = app.getNetAddresss('plugin.case-library.api.mobile.home.home-cut-category');
    app._postNetWork({
      url: urlStr,
      data:{
        cid:that.data.touchId || that.data.hotCategory[0].id,
        page:that.data.page
      },
      success: function (resdata) {
        wx.hideLoading({
          success: (res) => {},
        });
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            hotCasesList : that.data.hotCasesList.concat(res.data.hotCases.data),
            current_page : res.data.hotCases.current_page,
            last_page : res.data.hotCases.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.hideLoading({
          success: (res) => {},
        });
      }
    });
  }
});